package com.fqcar.fqdf.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.baidu.location.BDLocation;
import com.fqcar.fqdf.DFApp;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.adapter.CityListAdapter;
import com.fqcar.fqdf.adapter.HeaderRecyclerAndFooterWrapperAdapter;
import com.fqcar.fqdf.core.BaseRequestActivity;
import com.fqcar.fqdf.entity.CityEntity;
import com.fqcar.fqdf.entity.CityRespBean;
import com.fqcar.fqdf.entity.ResponseEntity;
import com.fqcar.fqdf.service.RequestOptService;
import com.fqcar.fqdf.util.Constants;
import com.fqcar.fqdf.util.ViewHolder;
import com.fqcar.fqdf.view.IndexBar.widget.IndexBar;
import com.fqcar.fqdf.view.suspension.SuspensionDecoration;
import com.fqdf.framework.android.net.response.NetBeanWrapper;
import com.fqdf.framework.android.util.DialogUtil;
import com.fqdf.framework.android.view.listener.RecycleItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CityListActivity extends BaseRequestActivity implements RecycleItemClickListener {

    @BindView(R.id.back)
    ImageButton back;
    @BindView(R.id.location_city_name)
    TextView locationCityName;
    //右侧边栏导航区域
    @BindView(R.id.indexBar)
    IndexBar mIndexBar;
    @BindView(R.id.list)
    RecyclerView list;
    //显示指示器DialogText
    @BindView(R.id.tvSideBarHint)
    TextView mTvSideBarHint;

    private CityListAdapter cityListAdapter;
    private HeaderRecyclerAndFooterWrapperAdapter mHeaderAdapter;
    private LinearLayoutManager mManager;
    private SuspensionDecoration mDecoration;
    private BDLocation location = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_city_list);
        ButterKnife.bind(this);

        location = ((DFApp)getApplication()).getCurLocation();
        if(location != null){
            locationCityName.setText("当前定位城市：" + location.getCity());
        }
        list.setLayoutManager(mManager = new LinearLayoutManager(this));

        cityListAdapter = new CityListAdapter(this);
        mHeaderAdapter = new HeaderRecyclerAndFooterWrapperAdapter(cityListAdapter) {
            @Override
            protected void onBindHeaderHolder(ViewHolder holder, int headerPos, int layoutId, Object o) {
                holder.setText(R.id.city_name, (String) o);
            }
        };
//        mHeaderAdapter.setHeaderView(R.layout.activity_item_city, "测试头部");

        list.setAdapter(mHeaderAdapter);
        list.addItemDecoration(mDecoration = new SuspensionDecoration(this, cityListAdapter.getOriginList()).setHeaderViewCount(mHeaderAdapter.getHeaderViewCount()));

        //如果add两个，那么按照先后顺序，依次渲染。
        list.addItemDecoration(new DividerItemDecoration(CityListActivity.this, DividerItemDecoration.VERTICAL));


        mIndexBar.setmPressedShowTextView(mTvSideBarHint)//设置HintTextView
                .setNeedRealIndex(true)//设置需要真实的索引
                .setmLayoutManager(mManager);//设置RecyclerView的LayoutManager

//        if(getMyApplication().getCurLocation() != null){
//            locationCityName.setText("当前定位城市：" + (getMyApplication().getCurLocation().getCity()));
//        }
        getCityList();

    }

    @OnClick(R.id.back)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }

    /**
     * 获取城市列表
     */
    public void getCityList() {
        getProgressDialog().show();
//        RequestOptService.getCityList(CityListActivity.this,location.getLatitude(),location.getLongitude(), getCityListListener, getDefaultErrorListener());
        RequestOptService.getCityList(CityListActivity.this,location.getLatitude(),location.getLongitude(), new Response.Listener<NetBeanWrapper<CityRespBean>>() {
            @Override
            public void onResponse(NetBeanWrapper<CityRespBean> response) {
                getProgressDialog().dismiss();
                if(handleBeanResponse(response)){
                    return;
                }
                CityRespBean cityRespBean = response.getModelData();
                List<CityEntity> cityList = cityRespBean.getCityList();
                mIndexBar.setmSourceDatas(cityList)
                        .setHeaderViewCount(mHeaderAdapter.getHeaderViewCount())//设置数据设置HeaderView数量
                        .invalidate();

                cityListAdapter.setList(cityList);
                mHeaderAdapter.notifyDataSetChanged();
                mDecoration.setmDatas(cityList);
            }
        },getDefaultErrorListener());
    }

    private Response.Listener<ResponseEntity> getCityListListener = new Response.Listener<ResponseEntity>() {

        @Override
        public void onResponse(ResponseEntity response) {
            DialogUtil.hideIndeterminateProgress(getProgressDialog());
            List<CityEntity> cityList = JSON.parseArray(JSON.toJSONString(response.modelData.get("cityList")), CityEntity.class);

            mIndexBar.setmSourceDatas(cityList)
                    .setHeaderViewCount(mHeaderAdapter.getHeaderViewCount())//设置数据设置HeaderView数量
                    .invalidate();

            cityListAdapter.setList(cityList);
            mHeaderAdapter.notifyDataSetChanged();
            mDecoration.setmDatas(cityList);
        }
    };

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable(Constants.Tag.CITY_ENTITY, cityListAdapter.getItem(position));
        intent.putExtras(bundle);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    public boolean onItemLongClick(View view, int position) {
        return false;
    }
    /**
     * 组织数据源
     *
     * @param data
     * @return
     */
    /*private void initDatas(final String[] data) {
        //延迟200ms 模拟加载数据中....
        getWindow().getDecorView().postDelayed(new Runnable() {
            @Override
            public void run() {
                cityList = new ArrayList<>();
                for (int i = 0; i < data.length; i++) {
                    CityEntity cityEntity = new CityEntity();
                    cityEntity.setCityName(data[i]);//设置城市名称
                    cityList.add(cityEntity);
                }

                mIndexBar.setmSourceDatas(cityList)//设置数据
                        .setHeaderViewCount(mHeaderAdapter.getHeaderViewCount())//设置HeaderView数量
                        .invalidate();

                cityListAdapter.setDatas(cityList);
                mHeaderAdapter.notifyDataSetChanged();
                mDecoration.setmDatas(cityList);
            }
        }, 200);

    }*/
}
